 

var actmodel={
  /**
   *  返回菜单加默认第一个选项
   * @param {*} config 
   */
  getnav( config){    
    var nav = [{ id: 0, name: "快递配送", isshow: false, val: 1},
    {id: 1,name: "社区团长",isshow: false, val: 2},
    {id: 2,name: "到店自提",isshow: false,val: 4},
    {id: 3,name: "同城配送",isshow: false,val: 8}    ];
    var navCur = -1
    var delivery_config = null
    if (config["delivery_config"]) {
      delivery_config = config["delivery_config"]
      var mode = parseInt(delivery_config["mode"])
      nav.forEach(function (item, idx) {
        if ((mode & item.val) == item.val) {
          nav[idx]["isshow"] = true
          if (navCur == -1) {
            navCur = idx;
          }
        }
      })
      if (delivery_config["expresstxt"]) {
        nav[0].name = delivery_config["expresstxt"];
      }
      if (delivery_config["chieftxt"]) {
        nav[1].name = delivery_config["chieftxt"];
      }
      if (delivery_config["storetxt"]) {
        nav[2].name = delivery_config["storetxt"];
      }
      if (delivery_config["intracitytxt"]) {
        nav[3].name = delivery_config["intracitytxt"];
      }
    }
    else {
      nav[0].isshow = true;
      navCur = 0
    }
    nav = nav.filter(item => {
      return item.isshow

    })
    var paytype = -1
    var allpaymentmethod = [{ val: 1, name: "微信支付" }, { val: 2, name: "余额支付" }]
    var paymentmethod = []
    console.log("fffffffffffffffffffffffconfig")

    console.log(config)
    var paymode=3
    if(config && config.integral && config.integral.payment && config.integral.payment.mode){
      paymode=config.integral.payment.mode
    }
    allpaymentmethod.forEach(item => {
      if ((item.val & paymode) == item.val) {
        if (paytype == -1) {
          paytype = item.val
        }
        if(item.val==2 && config["payment"]['balancepayname']){
          item['name']=config["payment"]['balancepayname'] +'支付'
         }
        paymentmethod.push(item)
      }
    })
    return {nav,navCur,delivery_config,paymentmethod,paytype};
  },
  /**
   * 
   */
  calcsamecity(intracity,distance){
   

    var calcsamecityinfo = {};
    var freight=0
    if (distance.val - intracity.maxdistance > 0) {
      calcsamecityinfo = { status: -1, message: "超出配送范围" }
    }
    else {
      var totalprice = 0
      var nextval = distance.val - intracity.start.distance
      totalprice -= intracity.start.price
      if (nextval > 0) {
        var nextmode = nextval % intracity.every.distance
        nextval -= nextmode
        nextval = nextval / intracity.every.distance
        if (nextmode > 0) {
          nextval++;
        }
        totalprice -= nextval * intracity.every.price
      }
      
      freight= totalprice * -100   
      calcsamecityinfo = { status:  1}  
    }
  return {calcsamecityinfo,freight}
  },
  /**
   *  计算订单价
   * @param {*} cartdadta 
   * @param {*} freight 
   * @param {*} actcoupon 
   */
  billpriceinfo(cartdadta,freight,actcoupon){

    // console.log("mini")
    
    var ilen = cartdadta.length;
    var totalprice = 0;
    var pronums = 0;
    var prototalprice = 0;
    var freight = parseInt(freight)
    var integral=0;
    for (var i = 0; i < ilen; i++) {
      pronums += cartdadta[i]["num"];
      if(cartdadta[i]["activityid"]!='-101'){
        integral += cartdadta[i]["integral"] * 1
        prototalprice += (cartdadta[i]["activityid"] > 0 ? cartdadta[i].activityprice : cartdadta[i].realprice) * cartdadta[i]["num"]
      }
    }
    totalprice = prototalprice +  freight
    var paymoney = prototalprice
    if (actcoupon) {
      if (actcoupon.discountmoney >= paymoney) {
        paymoney = 0
      }
      else {
        paymoney = paymoney - actcoupon.discountmoney;
      }
    }
    paymoney +=freight
    paymoney= Math.round(paymoney)    
    return {prototalprice,totalprice,pronums,paymoney,integral}
  }  
}
module.exports = actmodel;